package com.aiti.lulian.mapper;

import com.aiti.lulian.workflow.TaskInstance;
import com.aiti.lulian.workflow.TaskStatus;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface TaskInstanceMapper extends BaseMapper<TaskInstance> {

    default List<TaskInstance> findByTaskDefinitionId(String nodeId) {
        return selectList(new QueryWrapper<TaskInstance>()
                .eq("task_definition_id", nodeId));
    }

    default List<TaskInstance> findByTaskDefinitionIdAndInstanceId(String nodeId, String instanceId) {
        return selectList(new QueryWrapper<TaskInstance>()
                .eq("task_definition_id", nodeId)
                .eq("process_instance_id", instanceId));
    }

    default List<TaskInstance> findByProcessInstanceId(String processInstanceId) {
        return selectList(new QueryWrapper<TaskInstance>()
                .eq("process_instance_id", processInstanceId));
    }

    default List<TaskInstance> findByProcessInstanceIdAndStatus(
            String processInstanceId, TaskStatus status) {
        return selectList(new QueryWrapper<TaskInstance>()
                .eq("process_instance_id", processInstanceId)
                .eq("status", status.name()));
    }

    default List<TaskInstance> findByAssigneeIdAndStatus(String assigneeId, TaskStatus status) {
        return selectList(new QueryWrapper<TaskInstance>()
                .eq("assignee_id", assigneeId)
                .eq("status", status.name()));
    }

    @Select("SELECT * FROM dj_task_instance WHERE assignee_id = #{assigneeId} AND status = 'PENDING'")
    List<TaskInstance> findPendingTasksByAssignee(@Param("assigneeId") String assigneeId);


    default List<TaskInstance> findPendingTasksByAssignees(List<String> assigneeIds) {
        return selectList(new QueryWrapper<TaskInstance>()
                .in("assignee_id", assigneeIds)
                .eq("status", TaskStatus.PENDING.name()));
    }

}